//
//  JQHomeOtherItemVC.m
//  JQLive
//
//  Created by Bruce on 20/8/12.
//  Copyright © 2020年 JQLive. All rights reserved.
//

#import "JQHomeOtherItemVC.h"
#import "JQHomeCollectionViewCell.h"
//#import "JQAppDelegate.h"
#import "JQHomeCollectionReusableView.h"
#import "JQHomeBannerCollectionReusableView.h"
#import "UIView+Extension.h"
#import "JQHomeLiveListModel.h"

//#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)

@interface JQHomeOtherItemVC ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout, DZNEmptyDataSetSource>

@property(strong,nonatomic) UICollectionView *collectionView;
@property(strong,nonatomic) NSMutableArray *arr;
@property(nonatomic, assign) NSInteger pageNum;
@property(strong,nonatomic) UIView *rectCornerView;
@property (nonatomic, copy) NSArray<JQHomeLiveListRows *> *liveListRowArr;

@end

@implementation JQHomeOtherItemVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    //不要忘记初始化；
    self.arr = [[NSMutableArray alloc] init];
    for (int i = 0; i < 9; i++) {
        [self.arr addObject:[UIImage imageNamed:[[NSString alloc] initWithFormat:@"%d",i + 1]]];
    }
    
    UIView *rectCornerView = [[UIView alloc] initWithFrame:CGRectMake(0, MY_TopSafeMargin+10, SCREEN_WIDTH, 125-MY_TopSafeMargin-10+44)];
//    rectCornerView.backgroundColor = kColorWithHex(0xF2F4FC);//[UIColor whiteColor];
    rectCornerView.backgroundColor = [UIColor whiteColor];
    [rectCornerView round:20 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
    [self.view addSubview:rectCornerView];// 注销 出现 下拉在背景图上
    self.rectCornerView = rectCornerView;
    
    
    [self.view addSubview:self.collectionView];
    [self.collectionView.mj_header beginRefreshing];
    
    self.view.backgroundColor = [UIColor clearColor];
    self.collectionView.backgroundColor = [UIColor clearColor];
    //    self.collectionView.frame = self.view.bounds;
    
//    self.collectionView.frame = CGRectMake(0, MY_NavHeight+10, SCREEN_WIDTH, SCREEN_HEIGHT- MY_NavHeight-10 -MY_BottomTabbarHeight);
    
    
    //    self.collectionView.frame = CGRectMake(0, MY_TopSafeMargin+10, SCREEN_WIDTH, SCREEN_HEIGHT-MY_TopSafeMargin-MY_TabbarSafeBottomMargin-10);
    //    self.collectionView.contentOffset = CGPointMake(0, -64);
    //    [_collectionView round:20 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight];// 打圆角 存在 不能下拉的问题，故打圆角用上面的rectCornerView 单独做了个上半截角
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    
    //    UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120)];
    //    view.backgroundColor = kColorWithHex(0xF2F4FC);//[UIColor whiteColor];
    //    [self.collectionView addSubview:view];
    //    [self.collectionView insertSubview:view atIndex:0];
    //    [view round:20 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
    //    
    //    if(self.rectCornerView){
    //        [self.rectCornerView removeFromSuperview];
    //        self.rectCornerView = nil;
    //    }
}
#pragma mark - Getters and Setters
- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.emptyDataSetSource = self;
        MY_WEAK;
        _collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            MY_STRONG;
            strongSelf.pageNum++;
            [strongSelf loadData];
        }];
        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            MY_STRONG;
            strongSelf.pageNum = 1;
            [strongSelf loadData];
        }];
        [self.collectionView registerClass:[JQHomeCollectionViewCell class] forCellWithReuseIdentifier:@"MyCollectionCell"];
        [self.collectionView registerClass:[JQHomeCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
        [self.collectionView registerClass:[JQHomeBannerCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderBanner"];
    }
    return _collectionView;
}
#pragma mark - DZNEmptyDataSetSource 回调
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView{
    self.collectionView.backgroundColor = [UIColor clearColor];
    return 0;
}
// 数据加载
- (void)loadData{
    NSLog(@"数据加载 请求");
    
    //    page size roomTagId 房间标签ID get page=1&size=20
    NSDictionary *param = @{@"roomTagId":self.roomTagId, @"page":@(self.pageNum), @"size":@"20"};
    [MYRequesTool MY_live_rooms_appList_RequestWithParam:param successHandle:^(id  _Nonnull response) {
        NSLog(@"response2：%@", response);
        JQHomeLiveListModel *model = [JQHomeLiveListModel mj_objectWithKeyValues:response];
        if (model.code == 200) {
            if (model.data.rows.count == 0) {
                if (self.pageNum == 1) {
                    // 暂无数据
                    self.emptyString = @"暂无数据";
                    self.liveListRowArr = nil;
                    self.empty_isLoading = NO;
                    [self.collectionView reloadData];
                }else{
                    // 无更多数据
                    [self.collectionView.mj_footer endRefreshingWithNoMoreData];
                }
                return;
            }
            if (self.pageNum == 1) {
                self.liveListRowArr = [model.data.rows copy];
            }else{
                NSMutableArray *arrM = [NSMutableArray arrayWithArray:self.liveListRowArr];
                [arrM addObjectsFromArray:model.data.rows];
                self.liveListRowArr = [arrM copy];
            }
            self.empty_isLoading = NO;
            [self.collectionView reloadData];
        }
    } failureHandle:^(NSError * _Nonnull error) {
        NSLog(@"error:%@", error);
    }];
    
    
    [self endFreash];
}
#pragma mark - Private Methods
- (void)endFreash{
    if (self.collectionView.mj_header.isRefreshing) {
        [self.collectionView.mj_header endRefreshing];
    }
    if (self.collectionView.mj_footer.isRefreshing) {
        [self.collectionView.mj_footer endRefreshing];
    }
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"-----------    %lf",scrollView.contentOffset.y);
    
    // 设置下载距离
    if (scrollView.contentOffset.y <= -MY_TopSafeMargin-60) {
        //        scrollView.contentOffset.y = 17;
        [scrollView setContentOffset:CGPointMake(0,-MY_TopSafeMargin-60) animated:NO];
    }
    
    if (scrollView.contentOffset.y < -MY_TopSafeMargin-10+20) {
        self.collectionView.backgroundColor = [UIColor clearColor];
    }else{
        //        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.backgroundColor = kColorWithHex(0xF2F4FC);
    }
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    //  return self.arr.count;
    
    //    if (self.liveListRowArr.count == 0){
    //        return 2;
    //    }
    
    return self.liveListRowArr.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    JQHomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCollectionCell" forIndexPath:indexPath];
    //  cell.imageView.image = [self.arr objectAtIndex:indexPath.row];
    //    cell.titleLabel.text = [NSString stringWithFormat:@"测试 ……%ld", indexPath.row];
    //  cell.descLabel.text = [[NSString alloc] initWithFormat:@"%ld",indexPath.row];
    [cell setRowModelWith:self.liveListRowArr[indexPath.row]];
    return cell;
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusable = nil;
    if (kind == UICollectionElementKindSectionHeader) {
    }
    return reusable;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *message = [[NSString alloc] initWithFormat:@"你点击了第%ld个section，第%ld个cell",(long)indexPath.section,(long)indexPath.row];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //点击确定后执行的操作；
    }]];
    [self presentViewController:alert animated:true completion:^{
        //显示提示框后执行的事件；
    }];
}

#pragma mark - UICollectionViewDelegateFlowLayout
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    // return CGSizeMake((SCREEN_WIDTH - 24) / 2, (SCREEN_WIDTH - 24) / 2);
    return CGSizeMake((SCREEN_WIDTH - 26) / 2, (SCREEN_WIDTH - 26)*98/174 / 2 + 52);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    //    if ()
    return UIEdgeInsetsMake(20, 10, 0, 10);
    //    return UIEdgeInsetsMake(0, 0, 0, 0);
    //    return UIEdgeInsetsMake(20, 20, 10, 20);
}
// 横向
- (CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 6;
}
// 纵向
- (CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 6;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    //    if (section == 0) {
    //        return CGSizeMake(SCREEN_WIDTH,144 +42+20);
    //    }
    //  return CGSizeMake(300, 44);
    //    return CGSizeMake(SCREEN_WIDTH, 20);
    return CGSizeMake(0, 0);
}

@end

